package view;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;

import model.IExchangerModel;
import view.table.ExchangerTableCellEditor;
import view.table.ExchangerTableModel;
import view.table.ExchangerTableRenderer;
import controller.IExchangerController;

public class CurrenciesTableDialog extends JDialog implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IExchangerModel model;
	private ResourceBundleWrapper resourceBundleWrapper;
	private JTable table;
	private IExchangerController control;
	private JToolBar toolBar;
	private JButton selectAllButton, unSelectAllButton, deleteButton, addButton, closeButton;
	private AddCurrencyDialog addCurrenciesDialog;

	public CurrenciesTableDialog(IExchangerController control,
			IExchangerModel model, ResourceBundleWrapper resourceBundleWrapper) {
		super();
		this.model = model;
		this.control = control;
		this.resourceBundleWrapper = resourceBundleWrapper;
		this.setTitle(this.resourceBundleWrapper.getString("modify"));
		this.setSize(600, 500);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.buildContentPane();
		this.setVisible(true);
	}

	private void buildContentPane() {
		// Panel
		JPanel pan = new JPanel(new BorderLayout());
		// Create the toolbar.
		this.toolBar = new JToolBar();
		buildButtons();
		this.toolBar.setFloatable(false);
		this.toolBar.setRollover(true);
		pan.add(this.toolBar, BorderLayout.PAGE_START);
		// Table
		Object[][] data = model.getAllCurrenciesInfos();
		String title[] = { this.resourceBundleWrapper.getString("logo"),
				this.resourceBundleWrapper.getString("currencyName"),
				this.resourceBundleWrapper.getString("currencyCode"),
				this.resourceBundleWrapper.getString("rate") };
		this.table = new JTable(new ExchangerTableModel(data, title,
				this.resourceBundleWrapper.getString("select")));
		this.table.getTableHeader().setReorderingAllowed(false);
		this.table.setDefaultRenderer(String.class, new ExchangerTableRenderer(
				this.resourceBundleWrapper));
		this.table.setDefaultEditor(String.class, new ExchangerTableCellEditor(
				this.control, new JTextField()));
		pan.add(new JScrollPane(table), BorderLayout.CENTER);
		this.getContentPane().add(pan);
	}

	private void buildButtons() {
		selectAllButton = new JButton(this.resourceBundleWrapper.getString("selectAll"));
		selectAllButton.setToolTipText(this.resourceBundleWrapper.getString("selectAllButton"));
		selectAllButton.addActionListener(this);
		this.toolBar.add(selectAllButton);
		
		unSelectAllButton = new JButton(this.resourceBundleWrapper.getString("unSelectAll"));
		unSelectAllButton.setToolTipText(this.resourceBundleWrapper.getString("unselectAllButton"));
		unSelectAllButton.addActionListener(this);
		this.toolBar.add(unSelectAllButton);
		
		deleteButton = new JButton(this.resourceBundleWrapper.getString("delete"));
		deleteButton.setToolTipText(this.resourceBundleWrapper.getString("deleteButton"));
		deleteButton.addActionListener(this);
		this.toolBar.add(deleteButton);
		
		addButton = new JButton(this.resourceBundleWrapper.getString("add"));
		addButton.setToolTipText(this.resourceBundleWrapper.getString("addButton"));
		addButton.addActionListener(this);
		this.toolBar.add(addButton);
		
		closeButton = new JButton(this.resourceBundleWrapper.getString("close"));
		closeButton.setToolTipText(this.resourceBundleWrapper.getString("closeButton"));
		closeButton.addActionListener(this);
		this.toolBar.add(closeButton);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.closeButton) {
			this.dispose();
		} else if (e.getSource() == this.selectAllButton) {
			for (int i = 0; i < this.table.getRowCount(); i++)
				this.table.getModel().setValueAt(true, i, 0);

		} else if (e.getSource() == this.unSelectAllButton) {
			for (int i = 0; i < this.table.getRowCount(); i++)
				this.table.getModel().setValueAt(false, i, 0);

		} else if (e.getSource() == this.addButton) {
			addCurrenciesDialog = new AddCurrencyDialog(control, resourceBundleWrapper, this);
		} else if (e.getSource() == this.deleteButton) {
			int count = 0;
			for (int i = 0; i < this.table.getRowCount(); i++) {
				if ((Boolean) this.table.getModel().getValueAt(i, 0)) {
					String currencyName = (String) this.table.getModel()
							.getValueAt(i, 1);
					this.control.removeCurrency(currencyName);
					count++;
				}
			}
			if (count > 0) {
				this.refresh();
			}
		}
	}
	
	public void refresh(){
		((ExchangerTableModel) this.table.getModel())
		.refresh(this.model.getAllCurrenciesInfos());
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		Object[][] data = model.getAllCurrenciesInfos();
		String title[] = { this.resourceBundleWrapper.getString("logo"),
				this.resourceBundleWrapper.getString("currencyName"),
				this.resourceBundleWrapper.getString("currencyCode"),
				this.resourceBundleWrapper.getString("rate") };
		((ExchangerTableModel) this.table.getModel()).update(data, title);
		if (this.addCurrenciesDialog != null){
			this.addCurrenciesDialog.repaint();
		}
	}

}
